#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFileDialog>
#include <QDebug>
#include <QMessageBox>
#include <QTimer>
#include <QRegExp>

#include "upgradeform.h"
#include "configform.h"

#include "fileunit.h"
#include "serialport.h"
#include "datapack.h"
#include "msgparse.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE



class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void Sleep(int msec);

protected:
    void keyPressEvent(QKeyEvent *event); //键盘按下事件
    void keyReleaseEvent(QKeyEvent *event); //键盘松开事件
signals:
    void serialReadMsg(DataPack *data);

private slots:
    // 串口相关
    void serialReadData(char *data,qint64 len);
    void serialConnectErr(QString e);
    void on_reFlash_pressed();
    void on_openPort_pressed();
    void serialWriteData(uint8_t *data,int len);    // 串口数据接受的槽函数
    void serialWriteMsg(DataPack *data);             // 串口数据接受的槽函数

    void on_readBtn_clicked();
    void on_configBtn_clicked();

    void connectTimerCallBack(void);

private:
    Ui::MainWindow *ui;
    UpgradeForm *upgradeForm;
    ConfigForm *configForm;

    Serialport *serialport;
    int serialBaudList[4] = {4800,9600,38400,115200};
    uint8_t portConnectData[5] = {0xFE, 0x01, 0x00,0x00,0x01};
    uint8_t serialBaudIndex = 0;
    QTimer *checkPortTimer;   // 周期查询端口是否有改动
    QTimer *connectPortTimer; // 和下位机进行握手，确定波特率
    MsgParse *msgParse;     // 消息解析
    DataPack  *dataPack;    // 消息内容
};
#endif // MAINWINDOW_H
